/**
 * create by young (young2f@outlook.com) on 2023/6/18
 */
'use strict';
const {Engine} = require('json-rules-engine');


/**
 * Setup a new engine
 */
let engine = new Engine();

// define a rule for detecting the player has exceeded foul limits.  Foul out any player who:
// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes)
// engine.addRule({
//     conditions: {
//         any: [{
//             all: [{
//                 fact: 'gameDuration',
//                 operator: 'equal',
//                 value: 40
//             }, {
//                 fact: 'personalFoulCount',
//                 operator: 'greaterThanInclusive',
//                 value: 5
//             }]
//         }, {
//             all: [{
//                 fact: 'gameDuration',
//                 operator: 'equal',
//                 value: 48
//             }, {
//                 fact: 'personalFoulCount',
//                 operator: 'greaterThanInclusive',
//                 value: 6
//             }]
//         }]
//     },
//     event: {  // define the event to fire when the conditions evaluate truthy
//         type: 'fouledOut',
//         params: {
//             message: 'Player has fouled out!'
//         }
//     }
// })
//
// /**
//  * Define facts the engine will use to evaluate the conditions above.
//  * Facts may also be loaded asynchronously at runtime; see the advanced example below
//  */
// let facts = {
//     personalFoulCount: 6,
//     gameDuration: 40
// }
//
//
// /////
// let facts1 = {
//     personalFoulCount: 6,
//     gameDuration: 40
// }
//
// engine.addRule({
//     conditions: {
//         any: [{
//             all: [{
//                 fact: 'gameDuration',
//                 operator: 'equal',
//                 value: 40
//             }, {
//                 fact: 'personalFoulCount',
//                 operator: 'greaterThanInclusive',
//                 value: 5
//             }]
//         }, {
//             all: [{
//                 fact: 'gameDuration',
//                 operator: 'equal',
//                 value: 48
//             }, {
//                 fact: 'personalFoulCount',
//                 operator: 'greaterThanInclusive',
//                 value: 6
//             }]
//        }]
//     },
//     event: {  // define the event to fire when the conditions evaluate truthy
//         type: 'fouledOut',
//         params: {
//             message: 'Player has fouled out!'
//         }
//     }
// })
//
// // Run the engine to evaluate
// engine
//     .run(facts)
//     .then(({ events }) => {
//         console.log('events',events)
//         events.map(event => console.log('remsg:',event.params.message))
//     })

/**
 * test
 */
// engine.addRule({
//     name: 'testRule2',
//     conditions: {
//         all: [{
//             fact: 'v1',
//             operator: 'equal',
//             value: 1
//         },
//             {
//                 fact: 'v2',
//                 operator: 'equal',
//                 value: 1
//             },
//             {
//                 fact: 'v3',
//                 operator: 'equal',
//                 value: 1
//             }
//         ]
//     },
//     event: {
//         type: 'fouledOut',
//         params: {
//             message: 'testRule2 Player has fouled out!'
//         }
//     }
// });
engine.addRule({
    name: 'testRule1',
    conditions: {
        any: [
            {
                'fact': 'v2',
                'operator': 'equal',
                'value': 10,
                'name': 'v2'
            }
        ]
    },
    event: {
        type: 'fouledOut',
        params:'v2===10'
    }
});
engine.addRule({
    name: 'testRule2',
    conditions: {
        any: [
            {
                'fact': 'v1',
                'operator': 'equal',
                'value': 5,
                'name': 'v2'
            }
        ]
    },
    event: {
        type: 'fouledOut',
        params: 'v1===5'
    }
});

let facts2 = {
    v1: 5,
    v2: 100,
    v3: 1,
    SBMS: 5,
};
// Run the engine to evaluate
engine
    .run(facts2)
    .then(({events}) => {
        // events.map(event => console.log('remsg:', event.params.message));
        console.log(events);
    });
